import { PokerHand, cards2letter } from "./PokerHand.js";
import { PokerCard } from "../PokerCard.js";
export type { PokerCard, PokerType } from "../PokerCard.js";

/** 三带一 */
export class ThirdWithPairPokerHand extends PokerHand {
  public value: number;
  constructor(cards: PokerCard[]) {
    super(cards);
    this.value = this.cards[2].value;
  }

  /**
   * 判断是否为三带一：数值相同的三张牌 + 一张单牌或一对牌。例如： 333+6 或 444+99
   * @param cards 手牌
   * @returns
   */
  public static isStandard(cards: PokerCard[]) {
    if (cards.length !== 5) {
      return false;
    }
    /** 临时对象 */
    const tmp: { [key: string]: number } = {};
    cards.forEach((card) => {
      if (tmp[card.value]) {
        tmp[card.value] += 1;
      } else {
        tmp[card.value] = 1;
      }
    });

    return (
      Object.values(tmp)
        .sort((a, b) => a - b)
        .join("") === "23"
    );
  }
  public isStandard = ThirdWithPairPokerHand.isStandard;
  public isBetter(target: PokerHand): boolean {
    if (target instanceof ThirdWithPairPokerHand && target.cards.length === this.cards.length) {
      return this.value > target.value;
    }

    return false;
  }
}

export default ThirdWithPairPokerHand;
